package com.smart.community.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smart.community.common.base.result.ResponseResult;
import com.smart.community.common.base.result.ResultStatus;
import com.smart.community.common.common.qo.QueryConditionsQo;
import com.smart.community.common.common.qo.SysUserQo;
import com.smart.community.common.common.vo.SysUserVo;
import com.smart.community.common.service.SysUserService;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.websocket.server.PathParam;

/**
 * @author : 子未语
 * @date : 2024/1/13 9:56
 */
@RestController
@Validated
@RequestMapping("/sys/user")
@Api(tags = "用户管理api接口")
public class AdminUserController {
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/list")
    @ApiOperation("查询信息,并且分页展示")
    @ApiImplicitParams({@ApiImplicitParam(name = "queryConditionsQo", value = "搜索条件"), @ApiImplicitParam(name = "page", value = "当前页"), @ApiImplicitParam(name = "size", value = "每页条数")})
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "成功", response = SysUserVo.class), @ApiResponse(code = 5000, message = "失败")})
    public ResponseResult<IPage<SysUserVo>> searchAll(@RequestBody QueryConditionsQo queryConditionsQo, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size) {
        return ResponseResult.success(sysUserService.searchAll(queryConditionsQo, page, size));
    }


    @PostMapping("/modify/{id}")
    @ApiOperation("查询数据,根据id进行查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "需要查询的数据id")})
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "成功", response = SysUserVo.class), @ApiResponse(code = 5000, message = "失败")})
    public ResponseResult<SysUserVo> querySingle(@PathVariable @Min(value = 1, message = "参数无效") Integer id) {
        return ResponseResult.success(sysUserService.querySingle(id));
    }

    @PostMapping("/add")
    @ApiOperation("添加数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "sysUserQo", value = "需要添加的数据")})
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "成功", response = Boolean.class), @ApiResponse(code = 5000, message = "失败")})
    public ResponseResult<Boolean> addSysUser(@Valid @RequestBody SysUserQo sysUserQo) {
        return ResponseResult.success(sysUserService.addSysUser(sysUserQo));
    }


    @PostMapping("/update")
    @ApiOperation("修改啊用户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "sysUserQo", value = "需要修改的用户数据,里面需要包含id属性")})
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "成功", response = Boolean.class), @ApiResponse(code = 5000, message = "失败")})
    public ResponseResult<Boolean> modify(@Valid @RequestBody SysUserQo sysUserQo) {
        return ResponseResult.success(sysUserService.modify(sysUserQo));
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation("根据id进行了假删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "根据id进行删除的判断条件")})
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "成功", response = Integer.class), @ApiResponse(code = 5000, message = "失败")})
    public ResponseResult<Integer> del(@Min(value = 1, message = "请传入有效字段") @PathVariable Integer id) {
        return ResponseResult.success(sysUserService.del(id));
    }


    @PostMapping("/login")
    @ApiOperation("登录接口,登录成功则返回sessionId")
    @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "用户登录用户名"), @ApiImplicitParam(name = "password", value = "用户输入的密码")})
    public ResponseResult<String> doLogin(String username, String password) {
        return ResponseResult.success(sysUserService.login(username,password));
    }
}
